package com.bkgy.component;

import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.datacollect_bx.bean.JDataSave621;
import com.bkgy.modular.datacollect_bx.bean.JDataSave622;
import com.bkgy.modular.datacollect_bx.bean.JDataSave642;
import com.bkgy.controller.tbbxrecord.dao.TbBxFileSaveDao;
import com.bkgy.controller.tbbxrecord.entity.TbBxFileSave;
import com.bkgy.util.PdoUtils;
import com.bkgy.util.SFtpUtils;
import com.jcraft.jsch.JSchException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/dataSupply")
public class DataSupplyController {

    @Resource
    private PdoUtils pdoUtils;

    @Resource
    private SFtpUtils sFtpUtils;

    @Value("${sftp_host_621}")
    String host621;

    @Value("${sftp_host_622}")
    String host622;

    @Value("${sftp_host_642}")
    String host642;

    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private TbBxFileSaveDao tbBxFileSaveDao;

    @GetMapping("/tarSupply")
    public void dataSupply(@RequestBody JSONObject json){
        String line = json.getString("line");
        String extId = json.getString("extId");
        String year = json.getString("year");
        String month = json.getString("month");
        String day = json.getString("day");
        String path = "/dscdata/"+year+"/"+month+"/"+day+"/";
        if(line.equals("C621")){
            // 构建查询条件
            Query query = new Query(Criteria.where("extid").is(extId));
            List<JDataSave621> jDataSave621List = mongoTemplate.find(query, JDataSave621.class);
            // jDataSave622List长度大于等于1的时候直接返回,反之执行方法
            if (jDataSave621List.size() >= 1) {
                return;
            } else {
                String fileName = extId + ".json.tar";

                TbBxFileSave tbBxFileSave = new TbBxFileSave();
                tbBxFileSave.setLine("C621");
                tbBxFileSave.setCoil_id(extId);
                tbBxFileSave.setMessage("补录");
                tbBxFileSaveDao.insert(tbBxFileSave);
                pdoUtils.calPdo621(extId,tbBxFileSave);
                try {
                    sFtpUtils.calFile621(path, fileName, host621,tbBxFileSave);
                } catch (JSchException e) {
                    e.printStackTrace();
                }
            }
        }
        if(line.equals("C622")){
            // 构建查询条件
            Query query = new Query(Criteria.where("extid").is(extId));
            List<JDataSave622> jDataSave622List = mongoTemplate.find(query, JDataSave622.class);
            // jDataSave622List长度大于等于1的时候直接返回,反之执行方法
            if (jDataSave622List.size() >= 1) {
                return;
            } else {
                String fileName = extId + ".json.tar";
                TbBxFileSave tbBxFileSave = new TbBxFileSave();
                tbBxFileSave.setLine("C622");
                tbBxFileSave.setCoil_id(extId);
                tbBxFileSave.setMessage("补录");
                tbBxFileSaveDao.insert(tbBxFileSave);
                pdoUtils.calPdo622(extId,tbBxFileSave);
                try {
                    sFtpUtils.calFile622(path, fileName, host622,tbBxFileSave);
                } catch (JSchException e) {
                    e.printStackTrace();
                }
            }
        }
        if(line.equals("C642")){
            // 构建查询条件
            Query query = new Query(Criteria.where("extid").is(extId));
            List<JDataSave642> jDataSave642List = mongoTemplate.find(query, JDataSave642.class);
            // jDataSave642List长度大于等于1的时候直接返回,反之执行方法
            if (jDataSave642List.size() >= 1) {
                return;
            } else {
                String fileName = extId + ".json.tar";
                TbBxFileSave tbBxFileSave = new TbBxFileSave();
                tbBxFileSave.setLine("C642");
                tbBxFileSave.setCoil_id(extId);
                tbBxFileSave.setMessage("补录");
                tbBxFileSaveDao.insert(tbBxFileSave);
                pdoUtils.calPdo642(extId,tbBxFileSave);
                try {
                    sFtpUtils.calFile642(path, fileName, host642,tbBxFileSave);
                } catch (JSchException e) {
                    e.printStackTrace();
                }
            }
        }


    }

}
